<?php
namespace catetree;

class CateTree {
	// 无限级分类 
    public function catetree($cateRes){
        return $this->sort($cateRes);
    }

	// 无限级分类排序方法 递归循环调用
	public function sort($cateRes, $pid=0, $level=0){
		static $arr = array();
		foreach ($cateRes as $k => $v) {
			if($v['pid'] == $pid){
				$v['level']=$level;
				$arr[] = $v;
				$this->sort($cateRes, $v['id'], $level+1);
			}
		}
		return $arr;
	}

    // 获取子栏目id
	public function childrenids($cateid){
        $data = $this->field('id, pid')->select();
        return $this->_childrenids($data, $cateid);
    }
    private function _childrenids($data, $cateid){
    	static $arr = array();
    	foreach ($data as $k => $v) {
    		if($v['pid'] == $cateid){
                $arr[] = $v['id'];
    			$this->_childrenids($data, $v['id']);//递归查找子栏目。再找下一级的栏目。
    		}
    	}
    	// dump($arr);die;
    	return $arr;
    }

    //处理批量删除
    public function pdel($cateids){
    	// dump($cateids);
    	foreach ($cateids as $k => $v) {
    		// dump($v);
            $childrenidsArr = $this->childrenids($v);
            // dump($childrenidsArr);
            $childrenidsArr[] =(int)$v;//将要删除的栏目id加入数组。
            
        }
        $childrenidsArr = array_unique($childrenidsArr);
        dump($childrenidsArr);
        //进行数据删除
        $del = $this::destroy($childrenidsArr);
    }
}